<template>
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog
    :title="isEdit ? '编辑权限点' : '新增权限点'"
    :visible="showDialog"
    @close="btnCancel"
  >
    <!-- 表单 -->
    <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width: 90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width: 90%" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" style="width: 90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button
          size="small"
          type="primary"
          @click="confirmFn"
        >确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  props: {
    // 是否显示弹层
    showDialog: {
      type: Boolean
    },
    pid: {
      type: [Number, String],
      required: true
    },
    type: {
      type: [Number, String],
      required: true
    },
    formDataObj: {
      type: Object,
      default: (_) => {}
    },
    isEdit: {
      type: Boolean
    }
  },
  data() {
    const validateName = (rule, value, callback) => {
      let myArr = this.permissionList
        .filter((obj) => obj.pid === this.pid)
        .map((obj) => obj.name)
      if (this.isEdit) {
        myArr = this.permissionList
          .filter((obj) => obj.pid === this.pid && obj.id !== this.formData.id)
          .map((obj) => obj.name)
      }
      myArr.includes(value)
        ? callback(new Error(`此权限名${value}已存在`))
        : callback()
    }
    const validateCode = (rule, value, callback) => {
      let myArr = this.permissionList.map((obj) => obj.code)
      if (this.isEdit) {
        myArr = this.permissionList
          .filter((obj) => obj.id !== this.formData.id)
          .map((obj) => obj.code)
      }
      myArr.includes(value)
        ? callback(new Error(`此权限标识${value}已存在`))
        : callback()
    }
    return {
      permissionList: [], // 权限管理列表数据
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型吖
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    showDialog() {
      if (!this.isEdit) {
        this.formData.type = this.type
        this.formData.pid = this.pid
      }
    },
    formDataObj() {
      this.formData = this.formDataObj
    }
  },
  methods: {
    // 点击取消的事件
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.perForm.resetFields()
      this.$emit('close')
    },

    // 点击确定的点击事件
    async confirmFn() {
      this.$refs.perForm.validate(async(valid) => {
        if (valid) {
          this.$emit('confirmEv', this.formData)
        } else {
          this.$message.error('请输入正确的格式')
        }
      })
    }
  }
}
</script>

<style></style>
